package com.jintoufs.weixin.qrcode;

import com.alibaba.fastjson.JSONObject;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.util.CosUtil;
import com.jintoufs.util.http.HttpClientUtils;
import com.qcloud.cos.request.UploadFileRequest;
import io.swagger.annotations.Api;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@Api(value = "获取二维码")
@RestController
@RequestMapping("/api/qrcode")
public class Qrcode {

    @Resource
    private CosUtil cosUtil;
    // 接口A  适用于需要的码数量较少的业务场景
    String Url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN";
    //String Url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";
    //String Url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";
    @Resource
    private DictionaryService dictionaryService;

    //    @GetMapping("/getQrcode")
//    public String getQrcode(String path) {
//
//        String accessToken = dictionaryService.getAccessToken(Constants.MINIPROGRAM_ACCESSTOKEN);
//        if (!StringUtils.isEmpty(accessToken)) {
//            Url = Url.replace("ACCESS_TOKEN", accessToken);
//            Url = Url + "?path=" + "/pages/index/index-new";
//            String img = HttpClientUtils.http_get("", Url);
//        }
//        return Url;
//    }
    @GetMapping("/getQrcode")
    @ResponseBody
    public String getminiqrQr(String path, HttpServletResponse response) {
        RestTemplate rest = new RestTemplate();
        try {
            String accessToken = dictionaryService.getAccessToken(Constants.MINIPROGRAM_ACCESSTOKEN);
            String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;
            Map<String, Object> param = new HashMap<>();
            param.put("path", path);
            param.put("width", 430);
            param.put("auto_color", false);
            Map<String, Object> line_color = new HashMap<>();
            line_color.put("r", 0);
            line_color.put("g", 0);
            line_color.put("b", 0);
            param.put("line_color", line_color);
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            HttpEntity requestEntity = new HttpEntity(param, headers);
            ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
            byte[] bytes = entity.getBody();

            if(bytes==null) {
                System.out.println("生成二维码失败");
                return "";}

            UploadFileRequest uploadFileRequest = new UploadFileRequest("default", "/messageImage/" + System.currentTimeMillis() + "qrcode.png", bytes);

            StringBuilder attachment = new StringBuilder();
            try {
                String result = cosUtil.uploadToCos(uploadFileRequest);
                JSONObject object = JSONObject.parseObject(result);
                if ("0".equals(object.get("code").toString())) {
                    String filepath = JSONObject.parseObject(object.get("data").toString()).get("access_url").toString();
                    filepath = filepath.replace("http://", "https://");
                    attachment.append(filepath);
                } else {
                    attachment.append("error:").append(object.get("code").toString()).append(",");
                }
            } catch (Exception e) {
                e.printStackTrace();
                attachment.append("error").append(",");
            }
            return attachment.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
